package devkit.commando.rpc {
	
	import devkit.commando.ICommand;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	/**
	 * Command to invoke and return data from a remote service using a AMF remote procedure call.
	 * @author Francois Dempers
	 */
	public class RPC extends RPCBase implements ICommand {
		
		/** The name of the function in the service to invoke. */
		private var serviceFunction:String;
		
		/**
		 * Creates a new <code>RPC</code> command.
		 * @param gateway The gateway to connect through.
		 * @param service The path to the service to invoke.
		 * @param method The name of the server side function to invoke.
		 * @param resultHandler The client side function to invoke when a result is received.
		 * @param faultHandler The client side function to invoke when a fault is received.
		 */
		public function RPC(gateway:RPCGateway, service:String, method:String, resultHandler:Function=null, faultHandler:Function=null) {
			
			super(gateway, service, resultHandler, faultHandler);
			serviceFunction = method;
		}
		
		/**
		 * Executes the command.
		 * @param ...args The arguments to pass to the service. 
		 */
		public function execute(...args):void {
			
			call(serviceFunction, args);
		}
		
		/**
		 * Called when a result is received from the service.
		 * @param event The result event.
		 */
		public function result(event:ResultEvent):void {
			
			dispatchResult(event.result);
		}
		
		/**
		 * Called when a fault is received from the service.
		 * @param event The fault event.
		 */
		public function fault(event:FaultEvent):void {
			
			dispatchFault(HANDLE_BOTH, event);
		}
	}
}